<?php
/**
 * SdecoService
 */
namespace App\Service;

use Phalcon\Di;
use App\Models\Pics;
use App\Models\EwDeco;

class SdecoService  extends BaseService
{
	const MIGRATE_TRUE = 1;		//入库标识：是
	const MIGRATE_FALSE = 0;	//入库标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	/**
	 * [getList 列表 APP端列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['p.id,p.title,p.url,p.thumb,p.bigimg,p.source,p.create_time']);
		$builder->addFrom("App\Models\Pics", 'p');
		$builder->andWhere("p.is_del = " . self::DEL_FALSE);
		$builder->groupBy('p.url');
		//is_migrate 
		if (isset($params->is_migrate))
			$builder->andWhere("p.is_migrate = ". $params->is_migrate);

		//title
		if (isset($params->title) && $params->title)
			$builder->andwhere("p.title LIKE '%" . $params->title . "%'");

		//source
		if (isset($params->source) && $params->source)
			$builder->andwhere("p.source LIKE '%" . $params->source . "%'");

		//sources
		if (isset($params->sources) && $params->sources)
			$builder->andwhere("p.source LIKE '%" . $params->sources . "%'");
		
		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by, $table = "p");
		}else{
			$sql = " p.id desc";
		}
		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}
	
	/**
	 * [getInfo 详情]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getInfo($id)
	{
		return Pics::findFirst($id);
	}

	/**
	 * [delete 移除/恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = Pics::findFirst($id);

		if ($data){
			$data->is_del = $data->is_del ? self::DEL_FALSE : self::DEL_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [forever 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = Pics::findFirst($id);

		if ($row && $row->delete())
			return TRUE;

		return NULL;
	}

	/**
	 * [migrate 入库]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function migrate($id)
	{
		$info = Pics::findFirst($id);
		if (!$info )//|| $info->is_migrate == self::MIGRATE_TRUE
			return NULL;
		//获取url相同的数据 放到一个相册中
		$infos = Pics::find(['columns'=>'id,title,thumb,bigimg','conditions' => 'url = "' . $info->url .'"'])->toArray();

		$pics = [];
		$id_arr = [];
		foreach ($infos as $key => $value) {
			foreach ($value as $kk => $vv) {
				$value[$kk] = preg_replace("/\\n/", "", $vv);
				$value[$kk] = trim($value[$kk]);
			}
			$id_arr[] = $value['id'];
			// $model = Pics::findFirst($value['id']);
			// $model->is_migrate = self::MIGRATE_TRUE;
			// $model->save();
			unset($value['id']);
			$pics[$key] = $value;
		}

		foreach ($info as $key => $value) {
			$info->$key = preg_replace("/\\n/", "", $value);
			$info->$key = trim($info->$key);
		}
		$data = new \stdClass();
		$data->id_arr = $id_arr;
		$data->command = "SDECO";
		$data->city_id = 63;
		$data->title = $info->title;

		//2017-3-29 +
		$data->is_self = 0;
		$data->three_url = $info->url;
		
		// $data->tags = "";
		$data->cate_id = 7;
		$data->thumb = $info->thumb;
		$data->pics = json_encode($pics);
		// $data->comp_name = "";
		$data->sources = $info->source;
		// $data->visit = 0;
		// $data->comment = 0;
		// $data->is_display = 1; 
		// $data->is_del = 0;
		// $data->order_by = 100;
		// $data->create_time = date("Y-m-d H:i:s", time());
		// $data->update_time = date("Y-m-d H:i:s", time());
		// var_dump($data);die;
		$redis = Di::getDefault()['redis'];
		$queue = Di::getDefault()['queue'];

		$redis->set("SDECO_{$info->id}", $info->id);
		$redis->expireAt("SDECO_{$info->id}", time() + 60*5);
		$queue->put(json_encode((array)$data));

		// $row = new EwDeco;
		// // return $row -> save((array)$data);
		// if ($row->save((array)$data))
		// {
		// 	$info->is_migrate = self::MIGRATE_TRUE;
		// 	$info->save();
		// }
		// if (!$row->save((array)$data))
		// {
		// 	$msg =$row->getMessages();
		// 	var_dump($msg);die;
		// }
	}

	/**
	 * [batchMigrate 批量入库]
	 * @param  [type] $id_arr [description]
	 * @return [type]         [description]
	 */
	public static function batchMigrate($id_arr = [])
	{
		if ($id_arr && is_array($id_arr) && count($id_arr) > 0){
			foreach ($id_arr as $id) {
				self::migrate($id);
			}
		}
	}
}